package com.exp.practice.workbench.controller.teacher;


import com.exp.practice.base.exception.MyException;
import com.exp.practice.base.vo.PageVo;
import com.exp.practice.base.vo.ResultVo;
import com.exp.practice.workbench.domain.Question;
import com.exp.practice.workbench.service.teacher.TeacherPracticeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("workbench/teacher/practice")
public class TeacherPracticeController {

    @Resource
    private TeacherPracticeService teacherPracticeService;


    @RequestMapping("/addQuestion.do")
    @ResponseBody
    public ResultVo addQuestion(Question question){
        ResultVo resultVo=new ResultVo();
        //添加数据
        try {
            int count=teacherPracticeService.addQuestion(question);
            resultVo.setSuccess(true);
            resultVo.setMsg("添加题目成功");
        } catch (Exception e) {
            resultVo.setMsg(e.getMessage());
            resultVo.setSuccess(false);
        }
        return resultVo;
    }

    @RequestMapping("/pageList.do")
    @ResponseBody
    public PageVo pageList(String pageNo,String pageSize,Question question){

        PageVo pageVo=new PageVo();
       //得到总题目数
       int total=teacherPracticeService.getTotal(question);
       //得到所查询数据
        List<Question> list=teacherPracticeService.getQuestions(pageNo,pageSize,question);
        pageVo.setTotal(total);
        pageVo.setDataList(list);
        return pageVo;
    }

    @RequestMapping("/getQuestionById.do")
    @ResponseBody
    public ResultVo getQuestionById(String qid){
        ResultVo resultVo=new ResultVo();
        List<Question> list=teacherPracticeService.getQuestionById(qid);
        resultVo.setSuccess(true);
        resultVo.setDataList(list);
        return resultVo;
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public ResultVo update(Question question){
        ResultVo resultVo=new ResultVo();
        try {
            int count=teacherPracticeService.update(question);
            resultVo.setSuccess(true);
            resultVo.setMsg("修改成功");
        } catch (Exception e) {
            resultVo.setSuccess(false);
            resultVo.setMsg("修改失败");
        }
        return resultVo;
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public ResultVo delete(String[] qid){
        ResultVo resultVo=new ResultVo();
        try {
            int count=teacherPracticeService.delete(qid);
            resultVo.setSuccess(true);
            resultVo.setMsg("删除成功");
        } catch (MyException e) {
            resultVo.setSuccess(false);
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

}
